package com.itheima.server.controller;

import com.itheima.domain.mongo.Publish;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 阿鹏
 * @date 2021/1/28 14:58
 */
@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementService movementService;

    @PostMapping
    public ResponseEntity saveMovement(Publish publish, String location, MultipartFile[] imageContent) throws IOException {
        publish.setUserId(UserHolder.getUserId());
        publish.setLocationName(location);
        return movementService.saveMovement(publish, imageContent);
    }

    @GetMapping
    public ResponseEntity getMovement(@RequestParam(defaultValue = "1") int page,
                                      @RequestParam(defaultValue = "10") int pagesize) {
        return movementService.getMovement(page, pagesize);
    }

    @GetMapping("/recommend")
    public ResponseEntity findRecommend(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize) {
        return movementService.findRecommend(page, pagesize);
    }

    @GetMapping("/all")
    public ResponseEntity getAlbumList(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int pagesize,
                                       @RequestParam(required = false) Long userId) {
        return movementService.getAlbumList(page, pagesize, userId);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity saveLike(@PathVariable("id")String publishId) {
        return movementService.saveLike(publishId);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity removeLike(@PathVariable("id") String publishId) {
        return movementService.removeLike(publishId);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity saveLove(@PathVariable("id") String publishId) {
        return movementService.saveLove(publishId);
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity removeLove(@PathVariable("id") String publishId) {
        return movementService.removeLove(publishId);
    }


    @GetMapping("/{publishId}")
    public ResponseEntity findPublishDetail(@PathVariable("publishId") String publishId) {
        return movementService.findPublishDetail(publishId);
    }

    @GetMapping("/visitors")
    public ResponseEntity findVisitors() {
        return movementService.findVisitors();
    }
}
